/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> vv;
        queue<TreeNode*> q1;
        if (root) {
            q1.push(root);
        }
        while (!q1.empty())
        {
            int n = q1.size();
            vector<int> v;
            for (; n > 0; n--)
            {
                TreeNode* temp = q1.front();
                q1.pop();
                v.push_back(temp->val);
                if (temp->left) {
                    q1.push(temp->left);
                }
                if (temp->right) {
                    q1.push(temp->right);
                }
            }
            vv.push_back(v);
        }
        return vv;
    }
};